<?php
/**
 * Notes:
 * User: Administrator
 * Date: 2020/3/1
 * Time: 15:59
 * ${PARAM_DOC}
 * @return ${TYPE_HINT}
 * ${THROWS_DOC}
 */

namespace app\api\model;


use think\Db;
use think\Model;

class Feedback extends Model
{
    protected $name = 'feedback';

    /**
     * 反馈记录添加到数据库
     * @param $params
     */
    public function add_insert($params)
    {
        $params['createtime']=time();
        $insertGetId = Db::name('feedback')->strict(false)->insertGetId($params);
        return $insertGetId;

    }

    public function get_Index($id)
    {
        $cursor = Db::table('feedback')->where('user_id', $id)->cursor();
        return $cursor;

    }

    public static function findInfoByCondition($condition = [])
    {
        return self::where($condition)->find();
    }

    public static function saveData($condition = [], $data = [])
    {
        if ($condition && $data) {
            return self::where($condition)->update($data);
        }
    }

    /**
     * 获取根节点的最新一条记录
     * @param $condition
     * @return array
     */
    public static function findNewestRecord($condition = [])
    {
        return self::where($condition)->order('id desc')->find();
    }

    /**
     * 获取子列表
     */
    public static function dataChildList($params = [])
    {
        $where = [];
        $page = '1, 10';
        if (isset($params['page']) && $params['page_size']) {
            $page = $params['page'] . ',' . $params['page_size'];
        }
        if (isset($params['root_id'])) {
            $where['root_id'] = $params['root_id'];
        }
        $where['parent_id'] = ['gt', 0];
        $list = self::where($where)->page($page)->select();
        $listData = [];
        if ($list) {
            foreach ($list as $listVal) {
                $data = [
                    'id' => $listVal['id'],
                    'root_id' => $listVal['root_id'],
                    'parent_id' => $listVal['parent_id'],
                    'feedback_img' => $listVal['feedback_img'],
                    'nickmane' => $listVal['nickmane'],
                    'mobile' => $listVal['mobile'],
                    'feed_content' => $listVal['feed_content'],
                    'user_id' => $listVal['user_id'],
                    'time' => date('Y-m-d H:i:s', $listVal['createtime']),
                    'is_manager' => $listVal['user_id'] == 1 ? 1 : 0
                ];
                $listData[] = $data;
                unset($data);
            }
        }
        return $listData;
    }

}